package com.practicesuite.controllayer;

import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;

import com.practicesuite.systemlayer.Patient;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.User;

import java.awt.FlowLayout;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Panel for viewing a list of all patients, which can be selected to be deleted
 * or edited.
 * 
 * @author James
 *
 */
public class PatientAdministrationPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public MainWindow window;
    //public Patient patient1;
    //public MedicalInformation medicalInfo1;
    //public List<Patient> patients = new ArrayList<Patient>();
    public List<Patient> patients;
    Patient currentPatient;
    Patient targetPatient;
    String[] patientNames;
    

    /**
     * Create the panel.
     */
    public PatientAdministrationPanel(MainWindow container, final List<Patient> patients, final User user) {
        this.window = container;
        setLayout(new GridLayout(0, 1, 0, 0));
        setPreferredSize(new Dimension(500, 400));

        //medicalInfo1 = new MedicalInformation(41, "CVS", "Peanuts", "Blue Shield");
        //patient1 = new Patient("Ron", 41, 41, null, "123 Memory Lane", "6781234567", "Male", 22, medicalInfo1, "ron@gatech.edu");
        //patients.add(patient1);
        patientNames = new String[patients.size()];
        for (int i = 0; i < patients.size(); i++) {
            currentPatient = (Patient)patients.get(i);
            patientNames[i] = currentPatient.getName() + " (ID: " + currentPatient.getUserID() + ")";
        }
        
        JPanel panel = new JPanel();
        add(panel);
        panel.setLayout(new BorderLayout(0, 0));
        
        JPanel panel_1 = new JPanel();
        panel.add(panel_1, BorderLayout.NORTH);
        panel_1.setLayout(new GridLayout(0, 1, 0, 0));
        panel_1.setPreferredSize(new Dimension(500, 100));
        
        JLabel lblPatientList = new JLabel(" Patient List");
        lblPatientList.setHorizontalAlignment(SwingConstants.CENTER);
        panel_1.add(lblPatientList);
        lblPatientList.setFont(new Font("Tahoma", Font.PLAIN, 24));
        
        JPanel panel_2 = new JPanel();
        panel_1.add(panel_2);
        panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        
        final JList patientList = new JList(patientNames);
        patientList.setFont(new Font("Tahoma", Font.PLAIN, 12));
        patientList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        JScrollPane scrollPane = new JScrollPane(patientList);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(scrollPane);
        
        JButton btnEditPatientInfo = new JButton("Edit Patient Info");
        btnEditPatientInfo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int userIndex = patientList.getSelectedIndex();
                try {
                    targetPatient = (Patient)patients.get(userIndex);
                    window.gotoPanel("editPatient", targetPatient, user);
                } catch (IndexOutOfBoundsException ie) {
                    JOptionPane.showMessageDialog(null, "Please select a patient.");
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
                }
            }
        });
        panel_2.add(btnEditPatientInfo);
        btnEditPatientInfo.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JButton btnDeletePatient = new JButton("Delete Patient");
        btnDeletePatient.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int userIndex = patientList.getSelectedIndex();
                try {
                    targetPatient = (Patient)patients.get(userIndex);
                    SystemDriver.deleteUser(targetPatient.getUserID());
                    JOptionPane.showMessageDialog(null, "Patient account deleted successfully.");
                    int eUID = user.getEUID();
                    if (eUID == 1) {
                        window.gotoPanel("doctorHome", user);
                    }
                    else if (eUID == 2) {
                        window.gotoPanel("nurseHome", user);
                    }
                } catch (IndexOutOfBoundsException ie) {
                    JOptionPane.showMessageDialog(null, "Please select a patient.");
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
                }
            }
        });
        panel_2.add(btnDeletePatient);
        btnDeletePatient.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JButton btnCreatePatientAccount = new JButton("Create Patient Account");
        btnCreatePatientAccount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                window.gotoPanel("createPatient", user);
            }
        });
        panel_2.add(btnCreatePatientAccount);
        btnCreatePatientAccount.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JPanel panel_3 = new JPanel();
        panel.add(panel_3, BorderLayout.SOUTH);
        panel_3.setPreferredSize(new Dimension(500, 50));
        
        JButton btnReturnToHome = new JButton("Return to Home");
        btnReturnToHome.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int eUID = user.getEUID();
                if (eUID == 1) {
                    window.gotoPanel("doctorHome", user);
                }
                else if (eUID == 2) {
                    window.gotoPanel("nurseHome", user);
                }
            }
        });
        btnReturnToHome.setFont(new Font("Tahoma", Font.PLAIN, 15));
        panel_3.add(btnReturnToHome);

    }

}
